<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/13 0013
 * Time: 上午 10:40
 * Administrator
 */

namespace app\api\hook;


use app\common\util\ReturnMsgUtil;
use think\Hook;
use think\Request;
use think\Validate;

class ApiHook extends Hook
{
    use ReturnMsgUtil;
    public function run(Request $arg)
    {
        $valiClass = 'app\\'.$arg->module().'\\validate\\'.$arg->controller().'Validate';
        if (class_exists($valiClass)){          #存在验证类
            /**
             * @var $vali Validate
             */
            $vali = new $valiClass();
            if ($vali->hasScene($arg->action(true))) {  #存在验证场景
                if (!$vali->scene($arg->action(true))->check(input())) {
                    exit(self::errorInfo($vali->getError()));
                }
            }
        }else{
            $valiClass = 'app\\common\\validate\\'.$arg->controller().'Validate';
            if (class_exists($valiClass)){          #存在验证类
                /**
                 * @var $vali Validate
                 */
                $vali = new $valiClass();
                if ($vali->hasScene($arg->action(true))) {  #存在验证场景
                    if (!$vali->scene($arg->action(true))->check(input())) {
                        exit(self::errorInfo($vali->getError()));
                    }
                }
            }
        }
    }
}